package com.jiyun.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.dto.CaigouOrderDto;
import com.jiyun.entity.CaigouOrder;
import com.jiyun.entity.CaigouOrderMaterial;
import com.jiyun.service.ICaigouOrderMaterialService;
import com.jiyun.service.ICaigouOrderService;
import com.jiyun.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * 采购订单表 控制层
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-02-22
 */
@RestController
@RequestMapping("/caigou-order")
public class CaigouOrderController {

    @Autowired
    ICaigouOrderService caigouOrderService;


    @RequestMapping("findNewOrderId")
    public String findNewOrderId(){

        String orderId = caigouOrderService.findMaxOrderId();//最大的订单号

        String newId = orderId.substring(1);

        int num = Integer.parseInt(newId)+1;
        String newOrderId = "";
        if(num<10){
            newOrderId = "D0000"+num;
        }else if(num<100){
            newOrderId = "D000"+num;
        }else if(num<1000){
            newOrderId = "D00"+num;
        }

        return newOrderId;

    }

    @Autowired
    JwtUtil jwtUtil;
    /**
     * 添加接口
     */
    @RequestMapping("/add")
    public void addCaigouOrder(@RequestBody CaigouOrderDto caigouOrderDto, @RequestHeader("token") String token){


        CaigouOrder caigouOrder = new CaigouOrder();
        BeanUtils.copyProperties(caigouOrderDto,caigouOrder);

        caigouOrder.setShenheState("审核中");

        Claims claims = jwtUtil.parseJWT(token);
        LinkedHashMap user = claims.get("user", LinkedHashMap.class);
        caigouOrder.setBuyerUsername(user.get("username").toString());
        caigouOrder.setDept(user.get("dname").toString());

        caigouOrderService.save(caigouOrder);


        for (CaigouOrderMaterial caigouOrderMaterial : caigouOrderDto.getCaigouOrderMaterial()) {
            caigouOrderMaterial.setOrderId(caigouOrder.getOrderId());
            caigouOrderMaterialService.save(caigouOrderMaterial);
        }
    }

    @Autowired
    ICaigouOrderMaterialService caigouOrderMaterialService;

    /**
     * 修改接口
     */
    @RequestMapping("/update")
    public void updateCaigouOrder(@RequestBody CaigouOrder caigouOrder){
        caigouOrderService.updateById(caigouOrder);
    }

    /**
     * 单个删除接口
     */
    @RequestMapping("/deleteOne")
    public void deleteOne(@RequestParam Integer id){
        caigouOrderService.removeById(id);
    }

    /**
     * 批量删除接口
     * 前台ajax参数ids传入示例：1,2,3  (英文逗号分隔的字符串)
     */
    @RequestMapping("/deleteBatch")
    public void deleteBatch(@RequestParam List<Integer> ids){
        caigouOrderService.removeByIds(ids);
    }

    /**
     * 根据id查询实体接口
     */
    @RequestMapping("/findById")
    public CaigouOrder findById(@RequestParam Integer id){
        return caigouOrderService.getById(id);
    }

    /**
     * 查询所有接口
     */
    @RequestMapping("/findAll")
    public List<CaigouOrder> findAll(){
        return caigouOrderService.list();
    }

    /**
     * 分页查询接口
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     */
    @RequestMapping("/findPage")
    public Page<CaigouOrderDto> findPage(@RequestParam Integer page, @RequestParam Integer pageSize){

        return caigouOrderService.findPage(page,pageSize);
    }

}
